package com.movie.ticket.sale.bossassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.movie.ticket.sale.bossassembly.entity.BossAcount;
import com.movie.ticket.sale.bossassembly.entity.Cinema;
import com.movie.ticket.sale.bossassembly.mapper.BossAcountMapper;
import com.movie.ticket.sale.bossassembly.mapper.CinemaMapper;
import com.movie.ticket.sale.bossassembly.mapper.UserMapper;
import com.movie.ticket.sale.bossassembly.mapper.UserVipMapper;
import com.movie.ticket.sale.bossassembly.pram.CinemaPram;
import com.movie.ticket.sale.bossassembly.pram.UserVipPram;
import com.movie.ticket.sale.bossassembly.service.BossAcountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author muzi
 * @since 2020-12-09
 */
@Service
public class BossAcountServiceImpl extends ServiceImpl<BossAcountMapper, BossAcount> implements BossAcountService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserVipMapper userVipMapper;
    @Autowired
    private BossAcountMapper bossAcountMapper;
    @Autowired
    private CinemaMapper cinemaMapper;


    //查询会员用户占全部用户的百分比
    public UserVipPram isVip(){
        //查询所有的用户
        int userNumber = bossAcountMapper.findUserNumber();
        //查询看了会员的用户  去重
        int vipNumber = bossAcountMapper.findVipNumber();
        //存入对象中
        UserVipPram userVipPram = new UserVipPram();
        userVipPram.setUserNumber(userNumber);
        userVipPram.setVipNumber(vipNumber);
        // 创建一个数值格式化对象
        NumberFormat numberFormat = NumberFormat.getInstance();
        // 设置精确到小数点后2位
        numberFormat.setMaximumFractionDigits(2);
        String result = numberFormat.format((float)vipNumber/(float)userNumber*100);
        userVipPram.setIsVip(result+"%");
        return userVipPram;
    }

    public void deleteCinema(int cinemaId){
        //先根据id取出对象判断是否是院线
        Cinema cinema = cinemaMapper.selectById(cinemaId);
        if(cinema.getDegree() == 2){
            //直接修改装状态
            cinema.setState(-1);
            cinemaMapper.updateById(cinema);
        }
        //院线 关闭下面的影院
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("chain",cinemaId);
        List<Cinema> list = cinemaMapper.selectList(wrapper);
        //遍历集合 将集合 下面的影院状态全部改为 -1
        for (Cinema cinema1 : list){
            cinema1.setState(-1);
            cinemaMapper.updateById(cinema1);
        }
    }
    //展示影院或者院线的详细信息 院线需要展开
    public List<?> getCinema(int cinemaId){
        ArrayList<CinemaPram> cinemaPrams = new ArrayList<>();
        Cinema cinema = cinemaMapper.selectById(cinemaId);
            //直接将其返回 无论是否是院线 都会返回自己
            CinemaPram cinemas = bossAcountMapper.findCinemas(cinemaId);
            cinemaPrams.add(cinemas);
        //院线 找出 子类 全部返回
        if(cinema.getDegree() == 1){
            //查询子类
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("chain",cinemaId);
            List<Cinema> list = cinemaMapper.selectList(wrapper);
            //遍历 查数据
            for(Cinema cinema1 : list){
                //取ID
                CinemaPram cinemas1 = bossAcountMapper.findCinemas(cinema1.getId());
                cinemaPrams.add(cinemas1);
            }


        }


        return cinemaPrams;

    }


}
